package com.tencent.shadow.sample.host.ui.common;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;


import com.meichis.mcsappframework.dialog.LoadingDialog;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.tencent.shadow.sample.host.MCApplication;
import com.tencent.shadow.sample.host.R;
import com.tencent.shadow.sample.host.lifecycle.ProjectObserver;
import com.tencent.shadow.sample.host.utils.ActivityUtils;
import com.tencent.shadow.sample.host.utils.StatusBarUtil;
import com.tencent.shadow.sample.host.vm.ProjectVM;

/**
 * Created by chenhaifei on 2016/3/24.
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected String AuthKey = "";
    private LoadingDialog dialog_loading;
    public SharePreferenceUtil util;
    protected ProjectVM projectViewModel;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBar();
        beforeSetContentView();
        beforeInit();
        setDataBindingContentView();
        init();

        /**
         * 需要对所有Activity做公共生命周期操作的在ProjectObserver中处理
         */
        getLifecycle().addObserver(new ProjectObserver());
    }

    private void beforeSetContentView() {
        ActivityUtils.getInstance().addActivity(this);
        util = SharePreferenceUtil.getInstance();
        AuthKey = (String) util.getCacheValue(SharePreferenceUtil.PREFERENCES_AUTHKEY);
        projectViewModel = getAppViewModelProvider().get(ProjectVM.class);
    }

    protected void beforeInit(){}

    /**
     * 获取整个应用唯一的一份ViewModelProvider
     * @return
     */
    private ViewModelProvider getAppViewModelProvider(){
        return ((MCApplication)getApplicationContext()).getAppViewModelProvider(this);
    }

    protected abstract void setDataBindingContentView();

    protected abstract void init();

    protected void setStatusBar() {
        StatusBarUtil.setTranslucentStatus(this);
        StatusBarUtil.setRootViewFitsSystemWindows(this, false);
    }

    public void showLoading(int strRec) {
        showLoading(getResources().getString(strRec), true);
    }

    public void showLoading(boolean showLoading) {
        if(showLoading){
            showLoading(getResources().getString(R.string.loading), true);
        }else {
            hideLoading();
        }

    }

    public void showLoading(Boolean showLoading, boolean canCancel) {
        if(showLoading){
            showLoading(getResources().getString(R.string.loading), canCancel);
        }else {
            hideLoading();
        }
    }

    public void showLoading(String progressMsg, boolean canCancel) {
        if (this.isFinishing()) return;
        if (dialog_loading == null) {
            dialog_loading = new LoadingDialog(this);
        }
        if (!dialog_loading.isShowing()) {
            dialog_loading.show();
        }
        dialog_loading.setText(progressMsg);
        dialog_loading.setCancelable(canCancel);
    }

    public void hideLoading() {
        if (dialog_loading != null)
            dialog_loading.dismiss();
    }

    public void showMsg(String Msg) {
        MessageTools.showLongToast(Msg);
    }

    public void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }

    public void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(this, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
    }

    public void openActivity(Class<?> pClass, int flag) {
        Intent intent = new Intent(this, pClass);
        intent.setFlags(flag);
        startActivity(intent);
    }

    public void openIntentActivity(Class<?> pClass, Intent intent) {
        intent.setClass(this, pClass);
        startActivity(intent);
    }

    public void openActivityAndClose(Class<?> pClass) {
        openActivity(pClass, null);
        ActivityUtils.getInstance().FinishActivity(this.getLocalClassName());
    }

    public void openActivityAndClose(Class<?> pClass, Bundle pBundle) {
        openActivity(pClass, pBundle);
        ActivityUtils.getInstance().FinishActivity(this.getLocalClassName());
    }

    public void close() {
        ActivityUtils.getInstance().FinishActivity(this.getLocalClassName());
    }

    public Activity getActivity() {
        return this;
    }

    public void funBtnClick(View v){
        v.setEnabled(false);
        CountDownTimer timer = new CountDownTimer(1500, 1500) {

            @Override
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
                v.setEnabled(true);
            }
        };
        timer.start();
    }

    /**
     * Destroy all fragments and loaders.
     */
    @Override
    protected void onDestroy() {
        hideLoading();
        ActivityUtils.getInstance().FinishActivity(this.getLocalClassName());
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        close();
    }
}
